﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eassets.Service.Global;
using Eassets.Service;
using Eassets.Layered.Domain;
using Eassets.Layered.Domain.Global;

namespace Eassets.UI.GlobalReference
{
    public partial class AuthorizationGroupMemberForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetTransactionGroups();
                DataBindToAllGridViewInNotSelectedStatus();
            }
        }

        private void GetTransactionGroups()
        {
            AuthorizationGroupService authorizationGroupService = new AuthorizationGroupService();
            dropDownListAuthorizationGroup.DataSource = authorizationGroupService.GetAllAuthorizationGroup();
            dropDownListAuthorizationGroup.DataTextField = "Code";
            dropDownListAuthorizationGroup.DataValueField = "AuthorizationGroupId";
            dropDownListAuthorizationGroup.DataBind();
        }

        protected void dropDownAuthorizationGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropDownListAuthorizationGroup.SelectedValue == "0")
            {
                DataBindToAllGridViewInNotSelectedStatus();
            }
            else
            {
                GetAllUnmappedAuthorizationGroupMembers();
                GetAllmappedAuthorizationGroupMembers();
            }
        }

        private void DataBindToAllGridViewInNotSelectedStatus()
        {
            EmployeeService employeeService = new EmployeeService();
            List<Employee> employeesList = new List<Employee>();

            try
            {
                employeesList = employeeService.GetAllEmployee().Where(x => x.EmployeeType == "Employee").ToList();

                //Data Bind to gridViewAllEmployee
                gridViewAllEmployee.DataSource = employeesList;
                gridViewAllEmployee.DataBind();

                //Data Bind to gridViewMappedEmployees
                gridViewMappedEmployees.DataSource = null;
                gridViewMappedEmployees.DataBind();
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void GetAllUnmappedAuthorizationGroupMembers()
        {
            if (dropDownListAuthorizationGroup.SelectedIndex > -1)
            {
                AuthorizationGroupMemberService authorizationGroupMemberService = new AuthorizationGroupMemberService();
                List<long> empIdList = new List<long>();
                empIdList =
                    authorizationGroupMemberService.GetUnMappedEmpIdsByAuthorizationGroup(
                        Convert.ToInt64(dropDownListAuthorizationGroup.SelectedItem.Value)).ToList();

                EmployeeService employeeService = new EmployeeService();
                List<Employee> employeesList = new List<Employee>();
                List<Employee> employeesListMain = new List<Employee>();
                foreach (long id in empIdList)
                {
                    Employee employee = new Employee();
                    employee = employeeService.GetByIdEmployee(id);

                    employeesList.Add(employee);
                }
                employeesListMain = employeesList.Where(x => x.EmployeeType == "Employee").ToList();
                gridViewAllEmployee.DataSource = employeesListMain;
                gridViewAllEmployee.DataBind();

            }
            else
            {
                gridViewAllEmployee.DataSource = null;
                gridViewAllEmployee.DataBind();
            }
        }

        private void GetAllmappedAuthorizationGroupMembers()
        {
            if (dropDownListAuthorizationGroup.SelectedIndex > -1)
            {
                AuthorizationGroupMemberService authorizationGroupMemberService = new AuthorizationGroupMemberService();
                List<long> empIdList = new List<long>();

                empIdList =
                    authorizationGroupMemberService.GetEmpIdFromAuthorizationGroupMember(
                        Convert.ToInt64(dropDownListAuthorizationGroup.SelectedItem.Value)).ToList();

                EmployeeService employeeService = new EmployeeService();
                List<Employee> employeesList = new List<Employee>();
                foreach (long id in empIdList)
                {
                    Employee employee = new Employee();
                    employee = employeeService.GetByIdEmployee(id);

                    employeesList.Add(employee);
                }
                gridViewMappedEmployees.DataSource = employeesList;
                gridViewMappedEmployees.DataBind();
            }
            else
            {
                gridViewMappedEmployees.DataSource = null;
                gridViewMappedEmployees.DataBind();
            }
        }

        protected void checkBoxHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gridViewRow in ((GridView)((CheckBox)sender).Parent.Parent.Parent.Parent).Rows)
            {
                ((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked = ((CheckBox)sender).Checked;
            }
        }

        protected void buttonAddRight_Click(object sender, EventArgs e)
        {
            AuthorizationGroupMemberService authorizationGroupMemberService = new AuthorizationGroupMemberService();
            List<AuthorizationGroupMember> authorizationGroupMembers = new List<AuthorizationGroupMember>();
            AuthorizationGroupMember authorizationGroupMember;
            Boolean isSelected = false;
            foreach (GridViewRow gridViewRow in gridViewAllEmployee.Rows)
            {
                if (((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked)
                {
                    authorizationGroupMember = new AuthorizationGroupMember();
                    authorizationGroupMember.EmployeeId = Convert.ToInt64(gridViewRow.Cells[1].Text);
                    authorizationGroupMember.AuthorizationGroupId = Convert.ToInt64(dropDownListAuthorizationGroup.SelectedValue);

                    authorizationGroupMembers.Add(authorizationGroupMember);
                    isSelected = true;
                }
            }

            try
            {
                if (isSelected)
                {
                    authorizationGroupMemberService.MapAuthorizationGroupMember(authorizationGroupMembers);
                    FlashMessage1.Message = "Successfully Saved";
                    FlashMessage1.CssClass = "okmessage";
                    FlashMessage1.Display();
                }
                else
                {
                    FlashMessage1.Message = "No records are selected";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }
            }
            catch (Exception exception)
            {
                FlashMessage1.Message = "Saving Failed";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
            }

            GetAllUnmappedAuthorizationGroupMembers();
            GetAllmappedAuthorizationGroupMembers();

        }

        protected void buttonRemoveLeft_Click(object sender, EventArgs e)
        {
            AuthorizationGroupMemberService authorizationGroupMemberService = new AuthorizationGroupMemberService();
            //ArrayList authorizationGroupMemberEmpIds = new ArrayList();
            long selectedAuthGrpMemIds = 0;
            Boolean isSelected = false;

            foreach (GridViewRow gridViewRow in gridViewMappedEmployees.Rows)
            {
                if (((CheckBox)gridViewRow.Cells[0].FindControl("checkBoxItem")).Checked)
                {
                    selectedAuthGrpMemIds =
                        authorizationGroupMemberService.GetAuthorizationGroupMemberIdFromAuthorizationGroupMember(
                            Convert.ToInt64(gridViewRow.Cells[1].Text),
                            Convert.ToInt64(dropDownListAuthorizationGroup.SelectedItem.Value));
                    //authorizationGroupMemberEmpIds.Add(selectedAuthGrpMemIds);
                    isSelected = true;
                }
            }

            try
            {
                if (isSelected)
                {
                    authorizationGroupMemberService.DeleteAuthorizationGroupMember(selectedAuthGrpMemIds);
                    FlashMessage1.Message = "Successfully Saved";
                    FlashMessage1.CssClass = "okmessage";
                    FlashMessage1.Display();
                }
                else
                {
                    FlashMessage1.Message = "No records are selected";
                    FlashMessage1.CssClass = "errormessage";
                    FlashMessage1.Display();
                }

            }
            catch (Exception err)
            {
                FlashMessage1.Message = "Saving Failed";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
            }

            GetAllUnmappedAuthorizationGroupMembers();
            GetAllmappedAuthorizationGroupMembers();
        }
    }

}